package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.object.BaseConvert;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusParam;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.UserGroupBO;
import com.evildog.workspace.system.application.bo.UserGroupListBO;
import com.evildog.workspace.system.application.bo.UserGroupPageBO;
import com.evildog.workspace.system.application.convert.UserGroupConvert;
import com.evildog.workspace.system.application.model.UserGroupListParam;
import com.evildog.workspace.system.application.model.UserGroupPageParam;
import com.evildog.workspace.system.application.model.UserGroupParam;
import com.evildog.workspace.system.application.model.UserGroupUpdateRoleParam;
import com.evildog.workspace.system.application.service.UserGroupService;
import com.evildog.workspace.system.application.vo.UserGroupListVO;
import com.evildog.workspace.system.application.vo.UserGroupPageVO;
import com.evildog.workspace.system.application.vo.UserGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * userGroup Controller
 * 用户组
 * </p>
 *
 * @author evildog
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/user-groups")
@AllArgsConstructor
@Api(tags = "userGroup相关接口")
public class UserGroupController {

    private UserGroupService userGroupService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@Validated @RequestBody UserGroupParam userGroupParam) {
        return ResultGenerator.status(userGroupService.save(UserGroupConvert.INSTANCE.convert(userGroupParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(userGroupService.remove(id));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @Validated @RequestBody UserGroupParam userGroupParam) {
        return ResultGenerator.status(userGroupService.updateById(id, UserGroupConvert.INSTANCE.convert(userGroupParam)));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation(value = "更新status接口")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @Validated @RequestBody BaseStatusParam roleParam) {
        return ResultGenerator.status(userGroupService.updateStatus(id, BaseConvert.INSTANCE.convert(roleParam)));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询详情接口")
    public Result<UserGroupVO> getById(@PathVariable("id") Long id) {
        UserGroupBO userGroupBO = userGroupService.getDetailById(id);
        return ResultGenerator.success(UserGroupConvert.INSTANCE.convert(userGroupBO));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<UserGroupPageVO>> getPageList(UserGroupPageParam userGroupPageParam, PageQuery query) {
        PagedResult<UserGroupPageBO> userGroupPageBO = userGroupService.getPageList(
                query,
                QueryConditionUtils.getQueryCondition(userGroupPageParam),
                UserGroupConvert.INSTANCE.convert(userGroupPageParam));
        return ResultGenerator.success(UserGroupConvert.INSTANCE.convert(userGroupPageBO));
    }

    @GetMapping
    @ApiOperation(value = "表List查询接口")
    public Result<List<UserGroupListVO>> getList(UserGroupListParam userGroupListParam) {
        List<UserGroupListBO> userGroupListBO = userGroupService.getList(
                QueryConditionUtils.getQueryCondition(userGroupListParam),
                UserGroupConvert.INSTANCE.convert(userGroupListParam));
        return ResultGenerator.success(UserGroupConvert.INSTANCE.convertVoList(userGroupListBO));
    }

    @PutMapping("/{id}/roles")
    @ApiOperation(value = "更新关联角色接口")
    public Result<Boolean> updateRoles(@PathVariable("id") Long id, @RequestBody UserGroupUpdateRoleParam param) {
        return ResultGenerator.status(userGroupService.updateRoles(id, UserGroupConvert.INSTANCE.convert(param)));
    }

    /**
     * 获取用户关联的用户组
     */
    @GetMapping("/by-user")
    public Result<List<UserGroupListVO>> getList(@RequestParam Long userId) {
        List<UserGroupListBO> userGroupListBO = userGroupService.getList(userId);
        return ResultGenerator.success(UserGroupConvert.INSTANCE.convertVoList(userGroupListBO));
    }
}